package com.ball.web;

import com.alibaba.fastjson.JSONObject;
import com.ball.entity.User;
import com.ball.service.LogService;
import com.ball.service.UserService;
import com.ball.service.WalletService;
import com.ball.util.WalletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ball.tools.StringUtil.isNull;

@Controller
public class RegisterController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String,Object> rigster(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> map) throws InterruptedException {
        Map<String,Object> result =new HashMap<String,Object>();
        JSONObject json=new JSONObject(map);
        String username=json.getString("username");
        String password=json.getString("password");
        String mail=json.getString("mail");
        logger.info(password);
        //判断账号有没祖册
        if(!(username.equals("")||password.equals(""))){
            if(!StringUtils.isEmpty(userService.queryUserByUsername(json))){
                //账号已经注册
                result.put("result","2");
            }
            //注册账号
            else{
                User user =new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setMail(mail);
//                List<String> list=walletService.generate(json);
//                int i=0;
//                do{
//                try {
//
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                    i++;
//                }while (isNull(list.get(1))&&i<10);
//                System.out.println(list);
//                json.put("log",list.get(1));
//                logService.add(json);
//                String address=list.get(1).substring(50,150);
//                user.setWalletAddress(address);
                String money="0.00000000";
                user.setMoney(money);
                json.put("walletAddress","null");
                json.put("walletName",username);
                json.put("money",money);
                userService.add(json);
                result.put("result","1");
            }
        }else{
            result.put("result","0");
        }
        return result;
    }
}
